package com.ziq.zcamp.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.scwang.smart.refresh.horizontal.SmartRefreshHorizontal
import com.zhpan.bannerview.BannerViewPager2

class BannerReactionView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var bannerReactionBehavior : BannerReactionBehavior? = null

    fun updateBehavior(onSetting : (bannerReactionBehavior : BannerReactionBehavior?) -> Unit) {
        bannerReactionBehavior = BannerReactionBehavior()
        onSetting.invoke(bannerReactionBehavior)
        if(layoutParams is CoordinatorLayout.LayoutParams){
            (layoutParams as CoordinatorLayout.LayoutParams).behavior = bannerReactionBehavior
        }
    }


    class BannerReactionBehavior: CoordinatorLayout.Behavior<View>() {

        var onNestedPreScroll : ((
            coordinatorLayout: CoordinatorLayout,
            child: View,
            target: View,
            dx: Int,
            dy: Int,
            consumed: IntArray,
            type: Int ) -> Unit )? = null

        var onStopNestedScroll : ((
            coordinatorLayout: CoordinatorLayout,
            child: View,
            target: View,
            type: Int ) -> Unit )? = null


        override fun layoutDependsOn(
            parent: CoordinatorLayout,
            child: View,
            dependency: View
        ): Boolean {
            return dependency is BannerViewPager2<*> || dependency is SmartRefreshHorizontal
        }


        override fun onStartNestedScroll(
            coordinatorLayout: CoordinatorLayout,
            child: View,
            directTargetChild: View,
            target: View,
            axes: Int,
            type: Int
        ): Boolean {
            return true
        }

        override fun onNestedPreScroll(
            coordinatorLayout: CoordinatorLayout,
            child: View,
            target: View,
            dx: Int,
            dy: Int,
            consumed: IntArray,
            type: Int
        ) {
            onNestedPreScroll?.invoke(coordinatorLayout, child, target, dx, dy, consumed, type)
        }

        override fun onStopNestedScroll(
            coordinatorLayout: CoordinatorLayout,
            child: View,
            target: View,
            type: Int
        ) {
            onStopNestedScroll?.invoke(coordinatorLayout,
                child,
                target,
                type)
        }

    }

}